import Vue from 'vue'

import Cookies from 'js-cookie'

import Element from 'element-ui'
import '@/assets/styles/element-variables.scss'
import 'katex/dist/katex.min.css'
import '@/assets/styles/index.scss' // global css
import '@/assets/styles/ruoyi.scss' // ruoyi css
import '@/common/css/font.scss' //自定义字体注册
import App from './App'
import store from './store'
import router from './router'
import directive from './directive' // directive
import plugins from './plugins' // plugins
import { download, previewExcel } from '@/utils/request'

import './assets/icons' // icon
import './permission' // permission control
import { getDicts } from '@/api/system/dict/data'
import { getConfigKey } from '@/api/system/config'
import { renderedDescription } from './utils/util'
import {
  parseTime,
  resetForm,
  addDateRange,
  selectDictLabel,
  selectDictLabels,
  handleTree
} from '@/utils/cc'

import MathJax from './utils/MathJax'
// 分页组件
import Pagination from '@/components/Pagination'
// 自定义表格工具组件
import RightToolbar from '@/components/RightToolbar'
// 富文本组件以及公式和word插件
import Editor from '@/components/Editor'
// 文件上传组件
import FileUpload from '@/components/FileUpload'
// 图片上传组件
import ImageUpload from '@/components/ImageUpload'
// 图片预览组件
import ImagePreview from '@/components/ImagePreview'
// 字典标签组件
import DictTag from '@/components/DictTag'
// 头部标签组件
import VueMeta from 'vue-meta'
// 字典数据组件
import DictData from '@/components/DictData'
import tooltip from '@/components/tooltip'
import FormulaRenderer from './components/FormulaRenderer/FormulaRenderer'
// 课件类型组件
import courseType from '@/components/courseType'
import status from '@/components/status'
// 卡片组件
import card from '@/components/card'
import backContainer from '@/components/backContainer'
import handle from '@/components/handle'
import fileDrawer from '@/components/fileDrawer'
import cusImage from '@/components/cusImage'
// 打印组件
import printjs from 'print-js'
import 'print-js/dist/print.css'
import html2canvas from 'html2canvas'
import { registerEditorPlugins } from '@/plugins/editor-plugins'

// 注册编辑器插件
registerEditorPlugins()
//全局事件总线
Vue.prototype.$EventBus = new Vue()
// 剪裁图片组件
import vuecut from '@/components/vuecut'
// 在您的 Vue 组件中
import horizontalScrollDragDirective from './utils/horizontalScrollDragDirective.js'
// 注册自定义指令
Vue.directive('horizontal-scroll-drag', horizontalScrollDragDirective)
Vue.directive('preventReClick', {
  inserted(el, binding) {
    el.addEventListener('click', () => {
      if (!el.disabled) {
        el.disabled = true
        setTimeout(() => {
          el.disabled = false
        }, binding.value || 2000)
      }
    })
  }
})
// 解决element报错
Vue.directive('removeAriaHidden', {
  bind(el, binding) {
    let ariaEls = el.querySelectorAll('.el-radio__original')
    ariaEls.forEach((item) => {
      item.removeAttribute('aria-hidden')
    })
  }
})
import filters from '@/utils/filters' // global filters

// register global utility filters
Object.keys(filters).forEach((key) => {
  Vue.filter(key, filters[key])
})
import VScaleScreen from 'v-scale-screen'

const pickerOptions = {
  shortcuts: [
    {
      text: '最近一周',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
        picker.$emit('pick', [start, end])
      }
    },
    {
      text: '最近一个月',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
        picker.$emit('pick', [start, end])
      }
    },
    {
      text: '最近三个月',
      onClick(picker) {
        const end = new Date()
        const start = new Date()
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
        picker.$emit('pick', [start, end])
      }
    }
  ]
}
import { checkPhone, checkContent, formatUsername } from '@/utils/validate'

function toUrl(href) {
  window.open(href, '_blank')
}

function getBlob(url) {
  return new Promise((resolve) => {
    const xhr = new XMLHttpRequest()

    xhr.open('GET', url, true)
    xhr.responseType = 'blob'
    xhr.onload = () => {
      if (xhr.status === 200) {
        resolve(xhr.response)
      }
    }

    xhr.send()
  })
}

function saveAs(blob, filename) {
  let link = document.createElement('a')
  link.href = window.URL.createObjectURL(blob)
  link.download = filename
  link.click()
}

function openFile(url, name) {
  getBlob(url).then((blob) => {
    saveAs(blob, name)
  })
}

// 全局方法挂载
Vue.prototype.$openFile = openFile
Vue.prototype.$toUrl = toUrl
Vue.prototype.$renderedDescription = renderedDescription
Vue.prototype.$checkPhone = checkPhone
Vue.prototype.$checkContent = checkContent
Vue.prototype.$formatUsername = formatUsername
Vue.prototype.$pickerOptions = pickerOptions
Vue.prototype.$MathJax = MathJax
Vue.prototype.getDicts = getDicts
Vue.prototype.getConfigKey = getConfigKey
Vue.prototype.parseTime = parseTime
Vue.prototype.resetForm = resetForm
Vue.prototype.addDateRange = addDateRange
Vue.prototype.selectDictLabel = selectDictLabel
Vue.prototype.selectDictLabels = selectDictLabels
Vue.prototype.download = download
Vue.prototype.previewExcel = previewExcel
Vue.prototype.handleTree = handleTree
Vue.prototype.$print = printjs
Vue.prototype.$html2canvas = html2canvas
Vue.prototype.$isIncludesHtml = (str) => {
  // 检测各种 HTML 标签、属性、实体等
  const htmlRegex = /<[a-z][\s\S]*>|&[a-z0-9]+;/i
  return htmlRegex.test(str)
}

// 全局组件挂载
Vue.component('handle', handle)
Vue.component('fileDrawer', fileDrawer)
Vue.component('cusImage', cusImage)
Vue.component('courseType', courseType)
Vue.component('status', status)
Vue.component('card', card)
Vue.component('backContainer', backContainer)
Vue.component('vuecut', vuecut)
Vue.component('DictTag', DictTag)
Vue.component('tooltip', tooltip)
Vue.component('FormulaRenderer', FormulaRenderer)
Vue.component('Pagination', Pagination)
Vue.component('RightToolbar', RightToolbar)
Vue.component('Editor', Editor)
Vue.component('FileUpload', FileUpload)
Vue.component('ImageUpload', ImageUpload)
Vue.component('ImagePreview', ImagePreview)

Vue.use(directive)
Vue.use(plugins)
Vue.use(VueMeta)
Vue.use(VScaleScreen)
DictData.install()

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */

Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
})

Vue.config.productionTip = false

new Vue({
  el: '#app',
  router,
  store,
  render: (h) => h(App)
})
